package lookMessage;

import Bean.CheckBean;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import loginRegister.LoginBean;
import Bean.SqlBean;

/**
 *
 * @author 刘杉
 */
public class UpdatePasswordServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws  IOException {
        String password1=new String(request.getParameter("password1").getBytes("ISO-8859-1"),"UTF-8");
        String password2=new String(request.getParameter("password2").getBytes("ISO-8859-1"),"UTF-8");
        String password3=new String(request.getParameter("password3").getBytes("ISO-8859-1"),"UTF-8");
        HttpSession session=request.getSession();
        LoginBean login=(LoginBean)session.getAttribute("login");
        if(login==null){
            response.sendRedirect("http://localhost:8080/main/bottom.jsp");
        }
        else if(!CheckBean.is_numberOrAlpha(password1)||!CheckBean.is_numberOrAlpha(password2)||!CheckBean.is_numberOrAlpha(password3)){
            CheckBean.alert("密码由数字或字母组成!");
            response.sendRedirect("http://localhost:8080/lookMessage/updatePassword.jsp");
        }else if(!(password2.equals(password3))){
            CheckBean.alert("两次密码不同，修改失败！");
            response.sendRedirect("http://localhost:8080/lookMessage/updatePassword.jsp");
        }else if(!password1.equals(login.getPassword())){
            CheckBean.alert("原密码错误！");
            response.sendRedirect("http://localhost:8080/lookMessage/updatePassword.jsp");
        }else{
            try{
                ResultSet rs = null;
                SqlBean sb = new SqlBean();
                sb.connectSQL();
                String sql1 = "Update user set password='" + password2 + "' where userName='" + login.getUserName() + "'";
                sb.executeUpdate(sql1);
                login.setPassword(password2);
                session.setAttribute("login", login);
                sb.closeSQL();
                CheckBean.alert("填写信息合格，修改成功！");
                response.sendRedirect("http://localhost:8080/lookMessage/updatePassword.jsp");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws  IOException {
        doGet(request, response);
    }

}
